//
//  ZHFPlaceHeaderView.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFPlaceHeaderView.h"

#import "ZHFData.h"

#import "UIImageView+WebCache.m"

//#import "<#header#>"

@interface ZHFPlaceHeaderView ()<UIWebViewDelegate>

@property (nonatomic, retain) UILabel *nameLable;

@property (nonatomic, retain) UIView *backgView;

@property (nonatomic, retain) UIWebView *contentView;

@property (nonatomic, retain) UIView *lineView;

@property (nonatomic, retain) UILabel *titleName;

@property (nonatomic, retain) UIColor *color;

@end

@implementation ZHFPlaceHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        self.color = [UIColor colorWithRed:0.15 green:0.14 blue:0.16 alpha:1];
    }
    else {
        
        self.color = BACKGRONDCOLOR;
    }
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    [self addSubview:self.imageView];
    
    self.nameLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, SCREEN_WIDTH - 20, 50)];
    self.nameLable.textColor = [UIColor whiteColor];
    self.nameLable.font = [UIFont systemFontOfSize:32 weight:0.3];
    [self addSubview:self.nameLable];
    
    self.backgView = [[UIView alloc] initWithFrame:CGRectMake(0, 230, SCREEN_WIDTH, self.frame.size.height - 290 + 60)];
    self.backgView.backgroundColor = self.color;
    [self addSubview:self.backgView];
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(50, self.backgView.frame.size.height - 20, SCREEN_WIDTH - 100, 1)];
    self.lineView.backgroundColor = [UIColor colorWithRed:0.84 green:0.83 blue:0.79 alpha:1];
    [self.backgView addSubview:self.lineView];
    
    self.titleName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
    self.titleName.center = CGPointMake(SCREEN_WIDTH / 2, self.lineView.center.y);
    self.titleName.textColor = [UIColor colorWithRed:0.26 green:0.24 blue:0.19 alpha:1];
    self.titleName.text = @"推荐地点";
    self.titleName.textAlignment = NSTextAlignmentCenter;
    self.titleName.backgroundColor = self.color;
    self.titleName.font = [UIFont systemFontOfSize:15];
    [self.backgView addSubview:self.titleName];
    
    self.contentView = [[UIWebView alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 1)];
    self.contentView.delegate = self;
    [self.backgView addSubview:self.contentView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"states"] isEqualToString:@"1"]) {
        [self.contentView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#262429'"];
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
    }
    else {
        
        [self.contentView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FAF5E8'"];
    }
    
    CGRect frame = webView.frame;
    
    webView.scrollView.scrollEnabled = NO;
    webView.frame = frame;
    
    frame.size.height = webView.scrollView.contentSize.height;
    webView.frame = frame;
}

- (void)setArea:(ZHFData *)area
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:area.image_url] placeholderImage:[UIImage imageNamed:@"trip_edit_cover_default"]];
    self.nameLable.text = area.name_zh_cn;
    [self.contentView loadHTMLString:area.intros baseURL:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
 
 
 
*/

@end
